/*
 *
 * Copyright (C) 2012 Walter Lütgenau
 *
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 * 
 *      http://www.apache.org/licenses/LICENSE-2.0
 * 
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 * 
 */
package offset.nodes.client.editor.model;

import javax.swing.event.DocumentEvent;
import javax.swing.event.UndoableEditEvent;
import javax.swing.text.AttributeSet;
import javax.swing.text.Element;
import javax.swing.text.MutableAttributeSet;
import javax.swing.text.AbstractDocument.Content;
import javax.swing.text.AbstractDocument.DefaultDocumentEvent;
import javax.swing.text.DefaultStyledDocument.AttributeUndoableEdit;
import javax.swing.text.html.HTMLDocument;
import javax.swing.text.html.HTMLEditorKit;
import javax.swing.text.html.StyleSheet;

public class EditorDocument extends HTMLDocument {
	
    public EditorDocument() {
        super();
        // TODO Auto-generated constructor stub
    }

    public EditorDocument(Content c, StyleSheet styles) {
        super(c, styles);
        // TODO Auto-generated constructor stub
    }

    public EditorDocument(StyleSheet styles) {
        super(styles);
        // TODO Auto-generated constructor stub
    }

    public void setElementAttributes(Element e, AttributeSet s,
            boolean replace) {
        try {
            writeLock();

            // Make sure we send out a change for the length of the element.
            int offset = e.getStartOffset();
            int length = e.getEndOffset() - e.getStartOffset();

            DefaultDocumentEvent changes = new DefaultDocumentEvent(offset,
                    length, DocumentEvent.EventType.CHANGE);
            AttributeSet sCopy = s.copyAttributes();

            MutableAttributeSet attr = (MutableAttributeSet) e.getAttributes();
            changes.addEdit(new AttributeUndoableEdit(e, sCopy, replace));
            if (replace)
                attr.removeAttributes(attr);
            attr.addAttributes(s);

            changes.end();
            fireChangedUpdate(changes);
            fireUndoableEditUpdate(new UndoableEditEvent(this, changes));
        } finally {
            writeUnlock();
        }
    }

    /**
     * Overrides the default reader implementation to
     * enable ignoring HTML comments.
     * Such comments insert annoying views when used with MS IE copy and paste.
     *
     * @param pos the starting position
     * @return the reader used by the parser to load the document
     */
    public HTMLEditorKit.ParserCallback getReader(int pos) {
        super.getReader(pos);
        HTMLReader reader = new CommentIgnoringHTMLReader(pos);
        return reader;
    }

    class CommentIgnoringHTMLReader extends HTMLReader {

        public CommentIgnoringHTMLReader(int pos) {
            super(pos);
        }

        public void handleComment(char[] data, int pos) {
            boolean preserve = getPreservesUnknownTags();

            // do not preserve comments
            setPreservesUnknownTags(false);
            super.handleComment(data, pos);
            // reset preserve unknown tags handling to default for other tags
            setPreservesUnknownTags(preserve);
        }

    }

        
}
